package com.skp.shaphan.ui.dialogs;

import java.awt.Window;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;

import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.internalStorage.DBConnector;
import com.skp.shaphan.internalStorage.Fields;
import com.skp.shaphan.internalStorage.Tables;
import com.skp.shaphan.savedConnections.SavedConnection;
import com.skp.shaphan.savedConnections.SavedConnections;
import com.skp.shaphan.ui.dialogs.ConnectionDialogController.SavedConnectionCell;

public class DevOptions implements Initializable {
	@FXML CheckBox enableDev;
	@FXML CheckBox autoLogin;
	@FXML CheckBox useJavaFX;
	@FXML ComboBox<SavedConnection> autoLoginConnection;
	private Window window;
	private BooleanProperty optionDevEnabled = new SimpleBooleanProperty();
	private BooleanProperty optionAutoLogin = new SimpleBooleanProperty();
	private BooleanProperty optionUseJavaFX = new SimpleBooleanProperty();
	private ObjectProperty<SavedConnection> optionSavedConnection = new SimpleObjectProperty<SavedConnection>();

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		Connection conn = DBConnector.getInstance().getConnection();
		
		int autoLoginConnID = 0;
		try {
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from " + Tables.Dev);
			if(rs.next()) {
				optionDevEnabled.setValue(rs.getString(Fields.Dev_Enabled).equalsIgnoreCase("Y"));
				optionAutoLogin.setValue(rs.getString(Fields.Dev_AutoLogin).equalsIgnoreCase("Y"));
				optionUseJavaFX.setValue(rs.getString(Fields.Dev_UseJavaFX).equalsIgnoreCase("Y"));
				autoLoginConnID = rs.getInt(Fields.Dev_AutoConnectionId);
			}
			rs.close();
			st.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		SavedConnections sc = SavedConnections.getInstance();
		ObservableList<SavedConnection> conns = FXCollections.observableArrayList();
		for(SavedConnection c : sc.getConnections()) {
			conns.add(c);
			if(c.getId() == autoLoginConnID) {
				optionSavedConnection.setValue(c);
			}
		}
		autoLoginConnection.setItems(conns);
		autoLoginConnection.setCellFactory(new Callback<ListView<SavedConnection>, ListCell<SavedConnection>>() {
			
			@Override
			public ListCell<SavedConnection> call(ListView<SavedConnection> conn) {
				return new SavedConnectionCell();
			}
		});
		autoLoginConnection.setButtonCell(new SavedConnectionCell());
		
		enableDev.selectedProperty().bindBidirectional(optionDevEnabled);
		autoLogin.selectedProperty().bindBidirectional(optionAutoLogin);
		useJavaFX.selectedProperty().bindBidirectional(optionUseJavaFX);
		autoLoginConnection.valueProperty().bindBidirectional(optionSavedConnection);
	}
	
	public void attachFrame(Window javaFXConnectionDialog) {
		this.window = javaFXConnectionDialog;
	}
	
	@FXML
	public void cancel(ActionEvent e) {
		window.dispose();
	}
	
	@FXML
	public void ok(ActionEvent e) {
		try {
			Connection conn = DBConnector.getInstance().getConnection();
			Statement st = conn.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.FETCH_FORWARD);
			ResultSet rs = st.executeQuery("select * from " + Tables.Dev);
			if(rs.next()) {
				if(optionDevEnabled.get()) {
					rs.updateString(Fields.Dev_Enabled, "Y");
				} else {
					rs.updateString(Fields.Dev_Enabled, "N");
				}
				if(optionAutoLogin.get()) {
					rs.updateString(Fields.Dev_AutoLogin, "Y");
				} else {
					rs.updateString(Fields.Dev_AutoLogin, "N");
				}
				if(optionUseJavaFX.get()) {
					rs.updateString(Fields.Dev_UseJavaFX, "Y");
				} else {
					rs.updateString(Fields.Dev_UseJavaFX, "N");
				}
				rs.updateInt(Fields.Dev_AutoConnectionId, optionSavedConnection.getValue().getId());
				rs.updateRow();
			}
			rs.close();
			st.close();
			
			window.dispose();
			ShaphanApp.getMainWindow().setUseJavaFX(optionUseJavaFX.get());
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
	}
}
